import sys

from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QPalette, QColor
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton, QDialog, QMessageBox, \
    QHBoxLayout, QInputDialog

from base_pyqt.qwidget_ui import Ui_QWidget


class qwidget_main(Ui_QWidget, QWidget):
    # 自定义信号
    signal = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowType.MSWindowsFixedSizeDialogHint | Qt.WindowType.WindowStaysOnTopHint)

        # 窗口展示坐标及大小 默认正中央展示
        self.setGeometry(200, 400, 300, 100)
        self.palette = QPalette()
        self.palette.setColor(QPalette.ColorRole.Window, Qt.GlobalColor.red)
        self.lb = QLabel("====>color")

        self.lb.setAutoFillBackground(True)
        self.lb.setPalette(self.palette)
        # 添加组件及设置占比
        self.vbox.addWidget(self.lb, 1)

        self.edit = QTextEdit()
        self.edit.setText("color")

        # 添加间隔
        self.vbox.addStretch(1)
        self.vbox.addWidget(self.edit, 2)
        # 将Qwidget 设置为 vbox布局（随窗口变动而变动）
        self.setLayout(self.vbox)

        self.edit.setStyleSheet("background-color:white;color:black; font-size:20px")
        self.lb.installEventFilter(self)

        self.button = QPushButton()
        self.button.setText("dialog")
        self.button.setToolTip("点击弹框")
        self.vbox.addWidget(self.button)
        self.button.clicked.connect(self.show_dialog)

        # 信号绑定槽
        self.signal.connect(self.handleEvent)

    def show_dialog(self):
        reply = QMessageBox.information(self, "提示", '初始化错误',
                                        QMessageBox.StandardButton.Yes |
                                        QMessageBox.StandardButton.No)

        if reply == QMessageBox.StandardButton.Yes:
            print("yes")
            dialog = QInputDialog()
            text, ok = dialog.getText(self, "文本", "输入文本")
            if ok:
                self.edit.setText(text)




        elif reply == QMessageBox.StandardButton.No:
            print("no")

    # 槽，接收者响应信号时调用的方法，event 事件（信号）
    def moveEvent(self, event):
        pos = self.pos()
        print(f'window moved to : x={pos.x()},y={pos.y()}')
        # 发送者，发送自定义信号
        self.signal.emit(f"=======>moveEvent, x={pos.x()},y={pos.y()}")
        super().moveEvent(event)

    def resizeEvent(self, event):
        size = self.size()
        print(f"Window resized to: width={size.width()}, height={size.height()}")
        # 发送者，发送自定义信号
        self.signal.emit(f"=======>resizeEvent, x={size.width()},y={size.height()}")
        super().resizeEvent(event)

    def mousePressEvent(self, event):
        self.move(self.pos().x() + 100, self.pos().y() + 100)
        super().mouseMoveEvent(event)

    # 处理自定义信号的槽函数
    def handleEvent(self, event):
        print(event)

    # QWidget all event
    def event(self, event):
        print("Widget all event ")
        super().event(event)
        return True

    # 事件过滤器，通过 installEventFilter 注册
    def eventFilter(self, send, event):
        print(f'{type(send)}-----> {event.type()}')
        return super().eventFilter(send, event)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    main = qwidget_main()
    main.show()
    sys.exit(app.exec())
